// pages/detail/detail.js
const {eqdetail,fontlist,senddata} =require('../../utils/api')
const getNowDate = () => {
  var date = new Date();
  var sign2 = ":";
  var year = date.getFullYear() // 年
  var month = date.getMonth() + 1; // 月
  var day = date.getDate(); // 日
  var hour = date.getHours(); // 时
  var minutes = date.getMinutes(); // 分
  var seconds = date.getSeconds() //秒
 
 
  // 给一位数的数据前面加 “0”
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  if (hour >= 0 && hour <= 9) {
    hour = "0" + hour;
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = "0" + seconds;
  }
  return year + "-" + month + "-" + day ;
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    opof:0,
    uandd:0,
    eqtype:'',
    jingdu:'',
    weidu:'',
    landeangle:0,
    opoflistshow:false,
    uanddshow:false,
    landelistshow:false,
    grandsonshow:false,
    sonshow:false,
    coldlist:false,
    pavilionlist:false,
    downlistshow:false,
  modellistshow:false,
  typelistshow:false,
  lightmodellistshow:false,
  spraylistshow:false,
  starttime:'',
  endtime:'',
  lightstat:'',
  lightend:'',
  tap:0,
  show:true,
  list:[],
  id:'',
  eq:{},
  landelist:[],
  uanddlist:[],
  modellist:[
      {value: "1", label: "正常模式"},
      {
        value: "2", label: "调试模式"
         
      },
      {value: "7", label: "校准模式"},
  ],
  modellistson:[
    {value: "4", label: "演示运行"},
    {
      value: "3", label: "单步运行", 
      
    }
  ],
  modellistgrandson:[
    {value: "5", label: "角度运行"},
    {value: "6", label: "时间运行"}
  ],
  lightmodellist:[]

  

  },
  putopof(e){
    this.setData({
      'eq.openAndClose':e.detail.value
    })
  },
  opofitem(e){
    this.setData({
      opoflistshow:false,
      opof:e.currentTarget.dataset.op
    })
    console.log('开合方位是：',e.currentTarget.dataset.angle)
  },
  selectof(){
    
    this.setData({
      opoflistshow:true
    })
  },
  putuandd(e){
    this.setData({
      'eq.upAndDown':e.detail.value
    })
  },

  uandditem(e){
    this.setData({
      uanddshow:false,
      uandd:e.currentTarget.dataset.ud
    })
    console.log('上下方位是：',e.currentTarget.dataset.angle)
  },
  selectud(){
    this.setData({
      uanddshow:true
    })
  },
  putlandeangle(e){
    this.setData({
      'eq.localAngleEast':e.detail.value
    })
  },

  // 单击选择东西方位
  angleitem(e){
    this.setData({
      landeangle:e.currentTarget.dataset.angle,
      landelistshow:false
    })
    console.log('东西方位是：',e.currentTarget.dataset.angle)
  },
  sendangle(){
    let that=this
    wx.showModal({
      title: '提示',
      content: '确定下发吗',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
         
          let p={
            id:that.data.eq.id,
            peizhiStatus:4,
            ewStatus:that.data.landeangle,
            udStatus:that.data.uandd,
            openStatus:that.data.opof,
            eastAndWest:that.data.eq.eastAndWest,
            upAndDown:that.data.eq.upAndDown,
            openAndClose:that.data.eq.openAndClose

          }
          senddata(p).then(res=>{
            console.log(res)
            if(res.code==200){
              wx.showToast({
                title: '下发成功',
                icon: 'success',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
          
         

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  selectangle(){
    this.setData({
      landelistshow:true
    })
  },
  sendedowntime(){

    let that=this
    
    wx.showModal({
      title: '提示',
      content: '确定下发吗',
      success (res) {
        if (res.confirm) {
          if(that.data.eq.flatStatus==1){
            if(that.data.starttime==''){
              wx.showToast({
                title: '请选择开始时间',
                icon: 'none',
                duration: 2000
              })
              return
            }
           
              if(that.data.endtime==''){
                wx.showToast({
                  title: '请选择结束时间',
                  icon: 'none',
                  duration: 2000
                })
                return
              }
            let p={
              id:that.data.eq.id,
              peizhiStatus:5,
              flatStatus:that.data.eq.flatStatus,
              flatStart:getNowDate()+" "+that.data.starttime+':00',
              flatEnd:getNowDate()+" "+that.data.endtime+':00'
            }
            senddata(p).then(res=>{
              console.log(res)
              if(res.code==200){
                wx.showToast({
                  title: '下发成功',
                  icon: 'success',
                  duration: 2000
                })
              }else{
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            })
          
          }else{
            let p={
              id:that.data.eq.id,
              peizhiStatus:5,
              flatStatus:that.data.eq.flatStatus,
              flatStart:getNowDate()+" "+"00:00:00",
              flatEnd:getNowDate()+" "+"00:00:00"
            }
            senddata(p).then(res=>{
              console.log(res)
              if(res.code==200){
                wx.showToast({
                  title: '下发成功',
                  icon: 'success',
                  duration: 2000
                })
              }else{
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            })
          }
           
        
         
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  console.log(getNowDate())
   
  },
  putfengsu(e){
    this.setData({
      'eq.windThreshold':e.detail.value
    })
  },
  sendfengsu(){
    let that=this
    wx.showModal({
      title: '提示',
      content: '确定下发吗',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
         
          let p={
            id:that.data.eq.id,
            peizhiStatus:8,
            windThreshold:that.data.eq.windThreshold
           

          }
          senddata(p).then(res=>{
            console.log(res)
            if(res.code==200){
              wx.showToast({
                title: '下发成功',
                icon: 'success',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
          
         

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  sendcold(){
    let that=this
    wx.showModal({
      title: '提示',
      content: '确定下发吗',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
         
          let p={
            id:that.data.eq.id,
            peizhiStatus:9,
            snowStatus:that.data.eq.snowStatus,
            snowUpanddown:that.data.eq.snowUpanddown

          }
          senddata(p).then(res=>{
            console.log(res)
            if(res.code==200){
              wx.showToast({
                title: '下发成功',
                icon: 'success',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
          
         

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  putsnow(e){
    this.setData({
      'eq.snowUpanddown':e.detail.value
    })
  },
  coldop(e){
    this.setData({
      'eq.snowStatus':e.currentTarget.dataset.op,
      coldlist:false
    })
  },
  selpavilion(e){
    this.setData({
      pavilionlist:false,
      'eq.landscape':e.currentTarget.dataset.op
    })
  },
  longtime(e){
    this.setData({
      'eq.waterStart':e.detail.value
    })
  },
  itemtime(e){
    this.setData({
      'eq.waterEnd':e.detail.value
    })
  },
  showspray(){
    this.setData({
      spraylistshow:true
    })
  },
  sprayitem(e){
    this.setData({
      spraylistshow:false,
      'eq.waterStatus':e.currentTarget.dataset.op
    })
  },
  bindlightstart(e){
    this.setData({
      'eq.lightStart': e.detail.value
    })
    console.log(this.data.eq)
  },
  bindlightend(e){
    this.setData({
      'eq.lightEnd': e.detail.value
    })
  },
  yanshitime(e){
    this.setData({
      'eq.lightTime':e.detail.value
    })
  },
  coldlist(){
    this.setData({
      coldlist:true
    })
  },
  showlightmodel(){
    this.setData({
      lightmodellistshow:true
    })
  },
  lightmodelitem(e){
    console.log(e)
    this.setData({
      lightmodellistshow:false,
      'eq.lightMoshi':e.currentTarget.dataset.lightmodel
    })
  },
  sendwater(){
   
    let that=this
    wx.showModal({
      title: '提示',
      content: '确定下发吗',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
         
          let p={
            id:that.data.eq.id,
            peizhiStatus:7,
            waterStatus:that.data.eq.waterStatus,
            waterStart:that.data.eq.waterStart,
            waterEnd:that.data.eq.waterEnd

          }
          senddata(p).then(res=>{
            console.log(res)
            if(res.code==200){
              wx.showToast({
                title: '下发成功',
                icon: 'success',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
          
         

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
   
  },
  showlight(){
    this.setData({
      lightlistshow:true
    })
  },
  sendlighttime(){
    console.log('22222',this.data.eq)
    if(this.data.eq.lightStatus==1){
      console.log('等于2')
      if(this.data.eq.lightStart==null){
        wx.showToast({
          title: '请选择开启时间',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if(this.data.eq.lightEnd==null){
        wx.showToast({
          title: '请选择关闭时间',
          icon: 'none',
          duration: 2000
        })
        return
      }

    }
    let that=this
    wx.showModal({
      title: '提示',
      content: '确定下发吗',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          if(that.data.eq.lightStatus==1&&!that.data.eq.lightTime&&that.data.eq.lightMoshi=='2'){
            wx.showToast({
              title: '请输入延长时间',
              icon: 'none',
              duration: 2000
            })
           return
          }
          let p={
            id:that.data.eq.id,
            peizhiStatus:6,
            lightStatus:that.data.eq.lightStatus,
            lightStart:getNowDate()+" "+that.data.eq.lightStart+":00",
            lightEnd:getNowDate()+" "+that.data.eq.lightEnd+":00",
            lightMoshi:that.data.eq.lightMoshi,
            lightTime:that.data.eq.lightTime
          }
          senddata(p).then(res=>{
            console.log(res)
            if(res.code==200){
              wx.showToast({
                title: '下发成功',
                icon: 'success',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
          
         

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
   
  },
  lightitem(e){
    this.setData({
      lightlistshow:false,
      'eq.lightStatus':e.currentTarget.dataset.light
    })
    if(e.currentTarget.dataset.light==1){
      let p={
        dictType:'flo_equipment_light'
      }
      fontlist(p).then(res=>{
        console.log(res)
        this.setData({
          lightmodellist:res.data
        })
      })
    }
  },


  bindstarttime: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      starttime: e.detail.value
    })
  },
  bindendtime: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endtime: e.detail.value
    })
  },
  downitem(e){
    console.log(e)
    this.setData({
      downlistshow:false,
      'eq.flatStatus':e.currentTarget.dataset.of
    })
  },
  selectdown(){
    this.setData({
      downlistshow:true
    })
  },
  inputjingdu(e){
    this.setData({
      'eq.latitude':e.detail.value
    })
  },
  inputweidu(e){
    this.setData({
      'eq.longitude':e.detail.value
    })
  },
  sendmodel(e){
    let that=this
    wx.showModal({
      title: '提示',
      content: '确定下发吗',
      success (res) {
        if (res.confirm) {
          let p={
            id:that.data.eq.id,
            peizhiStatus:10,
            eqMoshi:that.data.eq.eqMoshi
          }
          senddata(p).then(res=>{
            console.log(res)
            if(res.code==200){
              wx.showToast({
                title: '下发成功',
                icon: 'success',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  sendlalo(){
    let that=this
    wx.showModal({
      title: '提示',
      content: '确定下发吗',
      success (res) {
        if (res.confirm) {
          let p={
            id:that.data.eq.id,
            peizhiStatus:0,
            latitude:that.data.eq.latitude,
            longitude:that.data.eq.longitude
          }
          senddata(p).then(res=>{
            console.log(res)
            if(res.code==200){
              wx.showToast({
                title: '下发成功',
                icon: 'success',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //选中模式并关闭下拉框
  modelitem(e){
    if(e.currentTarget.dataset.model==2){
      this.setData({
       sonshow:true
      })
    }else{
      this.setData({
        modellistshow:false,
        sonshow:false,
        grandsonshow:false,
        'eq.eqMoshi':e.currentTarget.dataset.model
      })
    }
    console.log(e)
    
  },
  modelson(e){
    if(e.currentTarget.dataset.model==3){
      this.setData({
        grandsonshow:true
      })
    }else{
      this.setData({
        modellistshow:false,
        sonshow:false,
        grandsonshow:false,
        'eq.eqMoshi':e.currentTarget.dataset.model
      })
    }
    console.log(e)
    
  },
  modelgrandson(e){
      this.setData({
        modellistshow:false,
        sonshow:false,
        grandsonshow:false,
        'eq.eqMoshi':e.currentTarget.dataset.model
      })
    
    
    console.log(e)
    
  },
  //显示模式下拉框
  selmodel(){
    this.setData({
      modellistshow:true
    })
   
  },
  //选中设备类型
  typeitem(e){
    console.log(e)
    this.setData({
      typelistshow:false,
     'eq.floType':e.currentTarget.dataset.dictvalue
    })
  },
  showpavilion(){
    this.setData({
      pavilionlist:true
    })
  },
  //下发设备类型
  sendeqtype(e){
     let that=this
    wx.showModal({
      title: '提示',
      content: '确定下发吗',
      success (res) {
        if (res.confirm) {
          let p={
            id:that.data.eq.id,
            peizhiStatus:3,
            floType:that.data.eq.floType,
            landscape:that.data.eq.landscape
          }
          senddata(p).then(res=>{
            console.log(res)
            if(res.code==200){
              wx.showToast({
                title: '下发成功',
                icon: 'success',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  //显示设备类型下拉列表框
  selecttype(){
    this.setData({
      typelistshow:true
    })
  },
  selectmodel(){

  },
  edit(){
    let p={
      dictType:'flo_equipment_type'
    }
    fontlist(p).then(res=>{
      console.log(res)
      if(res.code==200){
        this.setData({
          show:false,
          tap:6,
          list:res.data
        })
      }
    })
    let par={
      dictType:'flo_fangwei '
    }
    fontlist(par).then(res=>{
      console.log(res)
      this.setData({
        landelist:res.data
      })
    })
    let pa={
      dictType:'flo_shangxia'
    }
    fontlist(pa).then(res=>{
      console.log(res)
      this.setData({
        uanddlist:res.data
      })
    })
    let of={
      dictType:'flo_kaihe'
    }
    fontlist(of).then(res=>{
      console.log(res)
      this.setData({
        opoflist:res.data
      })
    })
   
  },
  edited(){
    this.setData({
      show:true,
      tap:0,
    
    })
  },

  handtap(e){
    this.setData({
      tap:e.currentTarget.dataset.tab
    })
  },
  settap(e){
    this.setData({
      tap:e.currentTarget.dataset.tab
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  console.log(options)
  this.setData({
    id:options.id
  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  let p={
    id:this.data.id
  }
  eqdetail(p).then(res=>{
    this.setData({
      eq:res.data
    })
    console.log('这是eq',this.data.eq)
  })

 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})